//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>

#include "BindingSitePredictionRefinement.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  BindingSitePredictionRefinement* bindingSitePredictionRefinement = NULL;
  int                              factor;

  if (argc < 6) {
    
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./RefineBindingSiteByPatterns <Filename (In) with Prediction of BS position> <Filename (Out) with Prediction of BS position refined  by models> <factor>";
    cout <<                               "<Filename (In) with Prediction of BS position by models for refinemet> <LingoSIM of Ligand in the model> ";
    cout <<                               "[<Filename (In) with Prediction of BS position by models for refinemet> <LingoSIM of Ligand in the model> ... ]";
    cout << endl;
    
    return EXIT_FAILURE;
    
  }
  else {
    
    cout << "Preparing files..." << endl;
    
    bindingSitePredictionRefinement = new BindingSitePredictionRefinement ();
    
    bindingSitePredictionRefinement->PredictionBindingSite()->FilenameIn()->In(argv[1]);
    bindingSitePredictionRefinement->FilenameOut()->In(argv[2]);
    
    for (int i = 4; i < argc; i = i + 2) {
      
      float lSim = 0;
      
      try {
        
        factor = atoi(argv[3]);
        lSim   = atof(argv[i + 1]);
        
      }
      catch (...) {
        
        if (bindingSitePredictionRefinement) 
          delete bindingSitePredictionRefinement;
        
        cout << "Error in the input parameters! Must by like: " << endl;
        cout << "./RefineBindingSiteByPatterns <Filename (In) with Prediction of BS position> <Filename (Out) with Prediction of BS position refined  by models> <factor>";
        cout <<                               "<Filename (In) with Prediction of BS position by models for refinement> <LingoSIM of Ligand in the model> ";
        cout <<                               "[<Filename (In) with Prediction of BS position by models for refinement> <LingoSIM of Ligand in the model> ... ]";
        cout << endl;

        return EXIT_FAILURE;
        
      }

      BindingSitePredictionData* newBindingSitePredictionData = new BindingSitePredictionData ();
      
      newBindingSitePredictionData->FilenameIn()->In(argv[i]);
      bindingSitePredictionRefinement->RefinementBindingSites()->Add(newBindingSitePredictionData);
      bindingSitePredictionRefinement->RefinementLingoSimilarity()->Add(lSim);
      
    }
    
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "                          Filename (In) with Prediction of BS position : " << argv[1] << endl;
    cout << "      Filename (Out) with Prediction of BS position refined  by models : " << argv[2] << endl;
    for (int i = 4; i < argc; i = i + 2) {
    cout << " Filename (In) with Prediction of BS position by models for refinement : " << argv[i] << endl;
    cout << "                                       LingoSIM of Ligand in the model : " << argv[i + 1] << endl;
    }
    cout << "---------------------------------------------------------------------------------" << endl;
    
  }

  cout << "Loading data..." << endl;
  if (!bindingSitePredictionRefinement->LoadData()) 
    cout << "Error loading data..." << endl;
  
  cout << "Refining the prediction of the Binding Sites using the input models..." << endl;
  if (!bindingSitePredictionRefinement->ComputeRefinePredictionBindingSite(factor))
    cout << "Error refining the prediction of the Binding Sites using the input models..." << endl;
  

  cout << "Writing results..." << endl;
  if (!bindingSitePredictionRefinement->OutToCSVFile())
    cout << "Error writing results..." << endl;
  
  cout << "Finished..." << endl << "Good Bye" << endl;
  
  if (bindingSitePredictionRefinement) delete bindingSitePredictionRefinement;

  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//


